/*
Copyright (C) 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

var OPTIONS_IDS_KEY = 'ids';

function Security() {
  this.exchange = '';
  this.ticker = '';
  this.last = 0;
  this.change = 0;
  this.changePercent = 0;
  this.lastDateTime = '';
  this.extendedLast = 0;
  this.extendedLastDateTime = '';
  this.extendedChange = 0;
  this.extendedChangePercent = 0;
  this.open = 0;
  this.high = 0;
  this.low = 0;
  this.volume = 0;
  this.averageVolume = 0;
  this.fiftyTwoWeekHigh = 0;
  this.fiftyTwoWeekLow = 0;
  this.marketCap = 0;
  this.name = '';
}

Security.DOW_TICKER = '.DJI';
Security.SP_TICKER = '.INX';
Security.NASDAQ_TICKER = '.IXIC';
Security.NYSE_TICKER = '.NYA';

Security.prototype.getDisplayName = function() {
  if (this.ticker == Security.DOW_TICKER) {
    return strings.DOW;
  } else if (this.ticker == Security.SP_TICKER) {
    return strings.SP;
  } else if (this.ticker == Security.NASDAQ_TICKER) {
    return strings.NASDAQ;
  } else if (this.ticker == Security.NYSE_TICKER) {
    return strings.NYSE;
  }

  return this.ticker;
};

Security.unserializeIds = function(data) {
  return data.split(',');
};

Security.serializeIds = function(ids) {
  return ids.join(',');
};

Security.prototype.load = function(data) {
  this.exchange = data.e;
  this.ticker = data.t;
  this.last = data.l;
  this.change = data.c;
  this.changePercent = data.cp;
  this.exchange = data.e;
  this.lastDateTime = data.lt;
  this.extendedLast = data.el;
  this.extendedLastDateTime = data.elt;
  this.extendedChange = data.ec;
  this.extendedChangePercent = data.ecp;
  this.open = data.op;
  this.high = data.hi;
  this.low = data.lo;
  this.volume = data.vo;
  this.averageVolume = data.avvo;
  this.fiftyTwoWeekHigh = data.hi52;
  this.fiftyTwoWeekLow = data.lo52;
  this.marketCap = data.mc;
  this.name = data.name;
};

Security.prototype.getId = function() {
  if (this.exchange) {
    return this.exchange + ':' + this.ticker;
  }

  return this.ticker;
};

Security.EXCHANGE_INDEX_PREFIX = 'INDEX';

Security.prototype.isIndex = function() {
  return this.exchange.substring(0, 5) == Security.EXCHANGE_INDEX_PREFIX;
};

Security.LARGE_CHANGE_THRESHOLD = 2;
Security.INDEX_LARGE_CHANGE_THRESHOLD = 1;

Security.prototype.isLargeMove = function() {
  var threshold = Security.LARGE_CHANGE_THRESHOLD;

  if (this.isIndex() || this.ticker == 'QQQQ') {
    threshold = Security.INDEX_LARGE_CHANGE_THRESHOLD;
  }

  return this.changePercent <= -threshold || this.changePercent >= threshold;
};

Security.prototype.isExtendedLargeMove = function() {
  var threshold = Security.LARGE_CHANGE_THRESHOLD;

  if (this.isIndex() || this.ticker == 'QQQQ') {
    threshold = Security.INDEX_LARGE_CHANGE_THRESHOLD;
  }

  return this.extendedChangePercent <= -threshold || this.extendedChangePercent >= threshold;
};

Security.prototype.isAfterHours = function() {
  return this.extendedLast && this.extendedLast !== 0;
};

Security.prototype.getDelayedMessage = function() {
  var message;

  if (this.isIndex()) {
    message = strings.DATA_REAL_TIME;
  } else if (this.exchange == 'NASDAQ' || this.exchange == 'NYSE') {
    message = Util.substitutePH(strings.EXCH_DATA_REAL_TIME,
        '[![EXCHANGE]!]',
        this.exchange);
  } else if (this.exchange== 'TSE') {
    message = Util.substitutePH(strings.DATA_15_MIN,
        '[![EXCHANGE]!]',
        this.exchange);
  } else if (this.exchange== 'AMEX') {
    message = Util.substitutePH(strings.DATA_20_MIN,
        '[![EXCHANGE]!]',
        this.exchange);
  } else if (!this.exchange) {
    message = strings.DATA_UNKNOWN_MIN;
  } else {
    message = Util.substitutePH(strings.EXCH_DATA_UNKNOWN_MIN,
        '[![EXCHANGE]!]',
        this.exchange);
  }

  return message;
};

Security.prototype.getPrettyExchange = function() {
  return this.isIndex() ? strings.N_A : this.exchange;
};

